mui.init();

//用户左滑  下一题
document.addEventListener("swipeleft", function () {
    var dataObj = brushing_data;
    console.log('前往下一题');
    var ty = dataObj.type;
    var id = dataObj.question_id;
    var ind = dataObj.q_index;
    console.log(ind);
    record_answer(ty, id, ind);

    console.log(dataObj.userAnswer);
    console.log(dataObj.Answering);

    var m = dataObj.q_index + 1;
    if (m == dataObj.other.count_num) {
        mui.toast('作答完毕，请提交答案查看结果!', {
            duration: 1000,
            type: 'div'
        });
        dataObj.open_dtk();
    } else {
        dataObj.q_index++; //翻到下一题
    }
});

//用户右滑  返回上一题
document.addEventListener("swiperight", function () {
    var dataObj = brushing_data;
    console.log('返回上一题');
    if (dataObj.q_index == 0) {
        mui.toast('该题目是第一题哦', {
            duration: 1000,
            type: 'div'
        });
    } else {
        var ty = dataObj.type;
        var id = dataObj.question_id;
        var ind = dataObj.q_index;
        console.log(ind);
        record_answer(ty, id, ind);

        console.log(dataObj.userAnswer);
        console.log(dataObj.Answering);

        dataObj.q_index--; //翻到上一题
    }
});

var brushing_data;
mui.plusReady(function () {
    var self = plus.webview.currentWebview();
    var catid = self.catid;
    brushing_data = new Vue({
        el: '#questions_box',
        data: {
            questionBoxStatus:0,
            questionErrorBoxStatus:0,
            errorTipsContent:"数据加载失败，可能网络异常",
            other: '',
            collection_status: 0,
            question_list: '',
            q_index: 0,
            type: 1,
            question_id: '',
            opIndex: '',
            userAnswer: [], //用户答题记录,该数组记录是否已经作答
            Answering: [], //记录用户选择了哪个选项的数组
            an: 0, //0表示该题目已回答  1表示没有回答

            cailiao_userAnswer: [], //记录材料题用户答题是否已经点击过提交按钮
            cailiao_Answering: [], //记录材料题用户答题记录 用于翻页时显示用户已作答的答案
            myAnswer: '', //用来显示用户已经填写提交过的答案
            myan: '', //用户填写答案的文本域双向绑定数据
            yes_num: 300,
            cailiaoTab: 0, //材料题切换选项卡的选项卡ID
            cailiaoCate: 'cailiao', //材料题切换选项卡的选项卡类型
            cailiao_question_id: '', //材料题问题的ID
            cailiao_question_parent_id: '', //材料题问题的对应材料ID
            wenti_index: 0,
        },
        mounted() {
            this.getOnesTestPaperDetails();
        },
        watch: {
            wenti_index: function (val, oldVal) {
                //记录切换之前那个下标问题的数据
                if (this.cailiao_userAnswer[oldVal] == 0) {
                    if (this.yes_num == 300) {
                        //表示用户没有输入任何文字
                        if (oldVal != -1) {
                            this.cailiao_userAnswer.splice(oldVal, 1, 0);
                        }
                    } else {
                        //用户输入过文字但是没有点击提交按钮
                        if (oldVal && oldVal >= 0) {
                            if (this.an == 0) {
                                this.cailiao_userAnswer.splice(oldVal, 1, 0);
                            } else {
                                //点击过提交按钮
                                this.cailiao_userAnswer.splice(oldVal, 1, 1);
                            }
                            this.cailiao_Answering[oldVal].splice(2, 1, this.myan);
                        }
                    }
                }

                //设置切换之后问题的回答
                if (val != -1) {
                    if (this.cailiao_userAnswer[val] == 1) {
                        this.myAnswer = this.cailiao_Answering[val][2];
                        this.an = 1;
                        this.myan = this.cailiao_Answering[val][2];
                    } else {
                        if (this.cailiao_Answering[val]) {
                            this.myan = this.cailiao_Answering[val][2];
                        } else {
                            this.myan = '';
                        }
                        this.myAnswer = '';
                        this.an = 0;
                    }
                }

                console.log(this.cailiao_userAnswer);
                console.log(this.cailiao_Answering);
            },
            q_index: function (val, oldVal) {
                if (val == brushing_data.other.count_num) {
                    mui.toast('作答完毕，请提交答案查看结果!', {
                        duration: 1000,
                        type: 'div'
                    });
                    brushing_data.open_dtk();
                    return false;
                }
                this.type = this.question_list[val].type;
                this.question_id = this.question_list[val].id;
                this.collection_status = this.question_list[val].collection_status;
                this.an = this.userAnswer[val];
                console.log('从下标' + oldVal + '到了下标' + val + '的数据');
                console.log('题目类型为：' + this.type);
                console.log('此时题目ID为：' + this.question_id);
                if (this.type == 1) {
                    console.log('切换到单选题了');
                    if (this.userAnswer[this.q_index] == 1 && this.Answering.length >= (this.q_index + 1)) { //说明改题目已经回答过
                        if (this.Answering[this.q_index].length < 3) {
                            $('.dan_option').removeClass('active');
                        } else {
                            var opid = this.Answering[this.q_index][2]; //获取该题目已经回答过的答案
                            if (opid) {
                                this.opIndex = opid;
                            } else {
                                brushing_data.$nextTick(function () {
                                    $('.dan_option').removeClass('active');
                                })
                            }
                        }
                    } else {
                        brushing_data.$nextTick(function () {
                            $('.dan_option').removeClass('active');
                        })
                    }
                } else if (this.type == 2) {
                    console.log('切换到多选题了');
                    if (this.userAnswer[this.q_index] == 1 && this.Answering.length >= (this.q_index + 1)) {
                        if (this.Answering[this.q_index].length < 3) {
                            brushing_data.$nextTick(function () {
                                $('.duo_option').removeClass('active');
                            })
                        } else {
                            brushing_data.$nextTick(function () {
                                var yid = this.Answering[this.q_index][2];
                                if (yid.length > 0) {
                                    var len = yid.length;
                                    for (var i = 0; i < len; i++) {
                                        $(".duoxuan_" + yid[i]).addClass('active');
                                    }
                                } else {
                                    $('.duo_option').removeClass('active');
                                }
                            })
                        }
                    } else {
                        brushing_data.$nextTick(function () {
                            $('.duo_option').removeClass('active');
                        })
                    }
                } else if (this.type == 3) {
                    console.log('切换到判断题了');
                    if (this.userAnswer[this.q_index] == 1 && this.Answering.length >= (this.q_index + 1)) {
                        if (this.Answering[this.q_index].length < 3) {
                            brushing_data.$nextTick(function () {
                                $('.dj_zq').removeClass('active');
                                $('.dj_cw').removeClass('active');
                            })
                        } else {
                            brushing_data.$nextTick(function () {
                                var asw = this.Answering[this.q_index][2];
                                if (asw == 1) {
                                    $('.dj_zq').addClass('active');
                                    $('.dj_cw').removeClass('active');
                                } else {
                                    $('.dj_cw').addClass('active');
                                    $('.dj_zq').removeClass('active');
                                }
                            })
                        }
                    } else {
                        brushing_data.$nextTick(function () {
                            $('.dj_zq').removeClass('active');
                            $('.dj_cw').removeClass('active');
                        })
                    }
                } else if (this.type == 30) {
                    if (this.userAnswer[val] == 1) {
                        this.myAnswer = this.Answering[val][2];
                        this.myan = this.Answering[val][2];
                        this.an = 1;
                    } else {
                        this.myAnswer = '';
                        this.myan = '';
                        this.an = 0;
                    }
                } else if (this.type == 50) {
                    console.log('5555555555555555');
                    this.cailiaoTab = this.question_list[val]['cl_li'][0].cailiao_index;
                    this.cailiaoCate = 'cailiao';
                    console.log(this.question_list);
                    console.log(val);
                    console.log(this.cailiaoTab);
                    this.cailiao_question_id = this.question_list[val]['question_li'][0].id;
                }
            },
            myan: function () {
                var str = this.myan;
                var len = str.replace(/[\u0391-\uFFE5]/g, "aa").length;
                this.yes_num = 300 - len;
            },
        },
        methods: {
            clickRefresh:function(){
                this.getOnesTestPaperDetails();
            },
            getOnesTestPaperDetails: function () {
                showWatting();
                var api_host = plus.storage.getItem('HOST_URL');
                var self = this;
                mui.ajax({
                    url: api_host + '/api/questionbank/choice_question/getOnesTestPaper',
                    data: {
                        'id': catid,
                    },
                    dataType: 'json',
                    type: 'get',
                    headers:{'User-Token':plus.storage.getItem('UserToken')},
                    success: function (msg) {
                        if (msg.code == 1) {
                            console.log(msg.data);
                            self.questionErrorBoxStatus = 0;
                            self.questionBoxStatus = 1;
                            self.question_list = msg.data.question;
                            self.type = self.question_list[0].type;
                            self.question_id = self.question_list[0].id;
                            self.other = msg.data.other;
                            self.collection_status = self.question_list[0].collection_status;
                            var c = self.other.count_num;
                            for (var i = 0; i < c; i++) {
                                self.userAnswer.splice(i, 1, 0);
                            }
                            var d = self.other.count_question_num;
                            for (var j = 0; j < d; j++) {
                                self.cailiao_userAnswer.splice(j, 1, 0);
                                self.cailiao_Answering.splice(j, 1, 0);
                            }
                            console.log(self.userAnswer);
                            console.log(self.cailiao_Answering);
                            plus.nativeUI.closeWaiting();
                            mui.currentWebview.show();
                        } else if (msg.code == 0) {
                            self.questionErrorBoxStatus = 1;
                            self.questionBoxStatus = 0;
                            self.errorTipsContent = msg.msg;
                            mui.toast(msg.msg, {duration: 1500,type: 'div'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        self.questionErrorBoxStatus = 1;
                        mui.toast('加载失败!', {duration: '200',type: 'div'});
                    },
                    complete:function () {
                        plus.nativeUI.closeWaiting();
                    }
                });
            },
            xuanzhong_danxuan_option: function (e) {
                //单选题点击选项
                this.opIndex = e.target.getAttribute('option-id');
                var qid = e.target.getAttribute('qid');
                this.an = 1;
                this.userAnswer.splice(this.q_index, 1, 1);
                if (this.Answering[this.q_index]) {
                    this.Answering[this.q_index].splice(2, 1, this.opIndex);
                } else {
                    var Answering = [this.type, qid, this.opIndex];
                    this.Answering.push(Answering);
                }


                console.log(brushing_data.userAnswer);
                console.log(brushing_data.Answering);

                mui.toast('进入下一题...', {
                    duration: 800,
                    type: 'div'
                });
                setTimeout(function () {
                    brushing_data.q_index++;
                }, 1000)
            },
            xuanzhong_duoxuan_option: function (e) {
                //多选题点击选项
                var opid = e.target.getAttribute('option-id');
                if ($('.duoxuan_' + opid).hasClass('active')) {
                    $('.duoxuan_' + opid).removeClass('active');
                } else {
                    $('.duoxuan_' + opid).addClass('active');
                }
            },
            dj_zq: function (e) {
                //判断题点击正确
                $('.dj_zq').addClass('active');
                $('.dj_cw').removeClass('active');
                this.an = 1;
                var id = e.target.getAttribute('qid');
                if (this.q_index + 1 == brushing_data.other.count_num) {
                    var answer = [3, id, 1];
                    if (this.userAnswer[this.q_index] == 0) {
                        this.Answering.push(answer);
                    } else {
                        this.Answering.splice(this.q_index, 1, answer);
                    }
                }
                // this.userAnswer.splice(this.q_index,1,1);
            },
            dj_cw: function (e) {
                var id = e.target.getAttribute('qid');
                //判断题点击错误
                $('.dj_cw').addClass('active');
                $('.dj_zq').removeClass('active');
                this.an = 1;
                if (this.q_index + 1 == brushing_data.other.count_num) {
                    var answer = [3, id, 0];
                    if (this.userAnswer[this.q_index] == 0) {
                        this.Answering.push(answer);
                    } else {
                        this.Answering.splice(this.q_index, 1, answer);
                    }
                }
                // this.userAnswer.splice(this.q_index,1,1);
            },
            open_dtk: function () {
                //显示答题卡
                $('.popMask').show();
                $('.popCard').animate({
                    'bottom': '0%'
                }, 300).show();
            },
            yc_dtk: function () {
                //隐藏答题卡
                $('.popMask').hide();
                $('.popCard').animate({
                    'bottom': '-100%'
                }, 300).hide(300);
            },
            add_collection: function () {
                questionCollection(this.question_id, this.question_list[this.q_index]['category'],brushing_data);
            },
            open_jc: function () {
                //打开纠错界面
                mui.openWindow({
                    url: 'errorCorrection.html',
                    extras: {
                        'question_id': this.question_id,
                        'category': this.question_list[this.q_index]['category']
                    },
                    createNew: false,
                    show: {
                        autoShow: false
                    }
                })
            },
            tijiaojieguo: function () {
                //提交并查看结果
                $('.popMask').hide();
                $('.popCard').animate({
                    'bottom': '-100%'
                }, 300).hide(300);
                plus.nativeUI.showWaiting('正在提交中...');
                batch_zg_question_post(catid);
                setTimeout(function () {
                    brushing_data.q_index = 0;
                    brushing_data.dtk_but_status = 0;
                    var len = brushing_data.userAnswer.length;
                    for (var i = 0; i < len; i++) {
                        brushing_data.userAnswer.splice(i, 1, 1);
                    }
                    var l = brushing_data.cailiao_userAnswer.length;
                    for (var j = 0; j < l; j++) {
                        brushing_data.cailiao_userAnswer.splice(j, 1, 1);
                    }
                    plus.nativeUI.closeWaiting();
                    console.log(brushing_data.userAnswer);
                    console.log(brushing_data.cailiao_userAnswer);
                }, 1000)
            },
            tiaozhuan_que: function (ind) {
                //点击答题卡小圆球跳转题目
                this.q_index = ind;
                this.yc_dtk();
            },
            tijiao_zuoda: function (type) {
                //简答题单个题目答案提交
                if (this.yes_num == 300) {
                    mui.toast('您还没有作答，无法提交哦！', {
                        duration: 1500,
                        type: 'div'
                    });
                    return false;
                } else {
                    // submit_question_answer(type,catid);
                    if (type == 'j') {
                        this.an = 1;
                        this.myAnswer = this.myan;
                        this.userAnswer.splice(this.q_index, 1, 1);
                        if (this.Answering[this.q_index]) {
                            this.Answering[this.q_index].splice(2, 1, this.myan);
                        } else {
                            var ans = [this.type, this.question_id, this.myan];
                            this.Answering.push(ans);
                        }
                        console.log(this.userAnswer);
                        console.log(this.Answering);
                    } else if (type == 'c') {
                        this.an = 1;
                        this.myAnswer = this.myan;
                        this.cailiao_userAnswer.splice(this.cailiaoTab, 1, 1);
                        if (this.cailiao_Answering[this.cailiaoTab]) {
                            this.cailiao_Answering[this.cailiaoTab].splice(2, 1, this.myan);
                        } else {
                            var ans = [this.type, this.cailiao_question_id, this.myan, this.question_id];
                            this.cailiao_Answering.splice(this.cailiaoTab, 1, ans);
                        }
                        console.log(this.cailiao_userAnswer);
                        console.log(this.cailiao_Answering);
                    }
                }

            },
            select_cailiaoTab: function (e) {
                if (e.target.getAttribute('tabCat') == 'wenti') {
                    this.cailiaoTab = e.target.getAttribute('qINdex');
                    this.cailiao_question_id = e.target.getAttribute('qid');
                    this.wenti_index = e.target.getAttribute('qINdex');
                } else if (e.target.getAttribute('tabCat') == 'cailiao') {
                    this.cailiaoTab = e.target.getAttribute('cINdex');
                    this.wenti_index = -1;
                }
                this.cailiaoCate = e.target.getAttribute('tabCat');

                console.log(this.cailiao_userAnswer);
                console.log(this.cailiao_Answering);
            },
            back_but: function () {
                //用户点击返回
                var btnArray = ['继续作答', '退出'];
                mui.confirm('当前练习未完成，退出将不会保存本次练习记录，确定退出？', '警告', btnArray, function (e) {
                    if (e.index == 0) {
                        mui.toast('取消退出', {
                            duration: 1500,
                            type: 'div'
                        });
                    } else {
                        mui.openWindow({
                            url: '../columns/questionBank.html'
                        })
                    }
                }, 'div')
            }
        }
    })
})

//记录用户选择的答案
function record_answer(ty, id, ind) {
    if (ty == 1) { //单选题
        var iii = ind + 1;
        console.log('正在记录单选题第' + iii + '题')
        if (brushing_data.userAnswer[ind] == 0) {
            console.log('单选题未选择滑动开始开始下一题');
            var Answering = [brushing_data.type, id, ''];
            brushing_data.Answering.push(Answering);
            brushing_data.userAnswer.splice(ind, 1, 0);
        }
    } else if (ty == 2) { //表示当前题目是多选题
        var iii = ind + 1;
        console.log('正在记录多选题第' + iii + '题')
        //表示该题目还未作答
        var obj = $('.duo_option');
        var id_arr = [];
        $.each(obj, function (index, value) {
            if ($(this).hasClass('active')) {
                id_arr.push($(this).attr('option-id'));
            }
        })
        if (id_arr.length == 0) {
            brushing_data.userAnswer.splice(ind, 1, 0);
        } else {
            brushing_data.userAnswer.splice(ind, 1, 1);
        }
        if (brushing_data.Answering[ind]) {
            //回答状态为0  答案数组中该项存在 表示用户返回重新作答之前没回答的题目
            brushing_data.Answering[ind].splice(2, 1, id_arr);
        } else {
            // 答案数组中该项不存在 表示是第一次作答 正常逻辑作答
            var answer = [ty, id];
            answer.push(id_arr);
            brushing_data.Answering.push(answer);
        }
    } else if (ty == 3) { //表示当前题目是判断题
        var iii = ind + 1;
        console.log('正在记录判断题第' + iii + '题')
        //返回作答之前没回答的
        if ($('.dj_zq').hasClass('active')) {
            var answer = [3, id, 1];
            brushing_data.userAnswer.splice(ind, 1, 1);
        } else if ($('.dj_cw').hasClass('active')) {
            var answer = [3, id, 0];
            brushing_data.userAnswer.splice(ind, 1, 1);
        } else {
            var answer = [3, id, ''];
            brushing_data.userAnswer.splice(ind, 1, 0);
        }
        if (brushing_data.Answering[ind]) {
            brushing_data.Answering.splice(ind, 1, answer);
        } else {
            brushing_data.Answering.push(answer);
        }
    } else if (ty == 30) {
        var iii = ind + 1;
        console.log('正在记录简答题第' + iii + '题');

        if (brushing_data.yes_num == 300) {
            brushing_data.userAnswer.splice(ind, 1, 0);
        } else {
            brushing_data.userAnswer.splice(ind, 1, 1);
        }

        if (brushing_data.Answering[ind]) {
            brushing_data.Answering[ind].splice(2, 1, brushing_data.myan);
        } else {
            var ans = [brushing_data.type, brushing_data.question_id, brushing_data.myan]
            brushing_data.Answering.push(ans);
        }
        console.log(brushing_data.userAnswer);
        console.log(brushing_data.Answering);
    } else if (ty == 50) {
        if (brushing_data.cailiao_userAnswer[brushing_data.cailiaoTab] == 1) {
            brushing_data.userAnswer.splice(ind, 1, 1);
        } else {
            brushing_data.userAnswer.splice(ind, 1, 0);
        }

        console.log(brushing_data.cailiao_userAnswer);
        console.log(brushing_data.cailiao_Answering);
    }
}


//批量提交答案到后台
function batch_zg_question_post(catid) {
    var api_host = plus.storage.getItem('HOST_URL');
    var userInfo = JSON.parse(plus.storage.getItem('userInfo'));
    mui.ajax({
        url: api_host + '/api/questionbank/choice_question/tt_batch_post',
        data: {
            'catid': catid,
            'mobile': userInfo.mobilenum,
            'kg_answer': brushing_data.Answering,
            'cailiao_answer': brushing_data.cailiao_Answering,
        },
        dataType: 'json', //服务器返回json格式数据
        type: 'post', //HTTP请求类型
        timeout: 10000, //超时时间设置为10秒；
        success: function (msg) {
            console.log(msg);
            mui.toast(msg.msg, {
                duration: '1000',
                type: 'div'
            });
        },
        error: function (xhr, type, errorThrown) {
            mui.toast('系统错误，请联系客服！', {
                duration: '2000',
                type: 'div'
            });
        }
    });
}

//简答题单个答案提交
/*
function submit_question_answer(type,catid) {
    var api_host = plus.storage.getItem('HOST_URL');
    var userInfo = JSON.parse(plus.storage.getItem('userInfo'));
    var user_token = plus.storage.getItem('user_token');
    if(type == 'j'){
        var qid = brushing_data.question_id;  //题目的ID
        var children_id = ''; //如果是材料题  就是这个问题的ID
    }else if(type == 'c'){
        var qid = brushing_data.question_id;
        var children_id = brushing_data.cailiao_question_id;
    }
    mui.ajax({
        url:api_host+'/api/questionbank/choice_question/zg_question_ones_answer',
        data:{
            'userToken':user_token,
            'mobile':userInfo.mobilenum,
            'question_id':qid,
            'children_id':children_id,
            'question_answer':brushing_data.myan,
            'category':3,
            'category_id':catid,
            'question_category':brushing_data.type,
        },
        dataType:'json',
        type:'post',
        success:function(msg){
            mui.toast(msg.msg,{ duration:'1000', type:'div'});
            if(msg.code == 1){
                brushing_data.an = 1;
                brushing_data.myAnswer = brushing_data.myan;
                brushing_data.userAnswer.splice(brushing_data.q_index,1,1);
            }else{
                brushing_data.an = 0;
            }
        },
        error:function(xhr,type,errorThrown){
            mui.toast('系统错误，请联系客服！',{ duration:'2000', type:'div'});
        }
    });
}*/